/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2006
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.persistence;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ListIterator;

import br.ufmg.dees.insane.assembler.Assembler;
import br.ufmg.dees.insane.assembler.FemAssembler;
import br.ufmg.dees.insane.model.femModel.element.Element;
import br.ufmg.dees.insane.model.femModel.node.Node;

/** A class representing a solver for FrameElements
 * @author Flavio
 * @since November 2006
 */
public class SolverFrame {
    
    public static void main(String[] args) {
        String fileName = "PFrameInput_xsd";
		/*if(args.length != 0){
			fileName = args[0];
		}
		else{
			System.out.println("Please, enter input file name\n");
			fileName = Teclado.lerString();
			fileName = fileName.trim();
		}*/
		PersistenceAsXML_XSD p = new PersistenceAsXML_XSD();
		PersistenceAsInsane o = new PersistenceAsInsane();
        
		// Getting the file extension
		String ext = new String();
		int i = fileName.lastIndexOf('.');
		if (i > 0 &&  i < fileName.length() - 1) {
			ext = fileName.substring(i+1).toLowerCase();
		}
		
		//Comparing the extension
		if (!ext.equals("xml"))
			fileName = fileName + ".xml";
			
		p.fillInsaneFromFile(fileName);
        
        Assembler a = new FemAssembler();
        a.setModel(p.getModel());
        
        p.getModel().addObserver(p);
        p.getModel().addObserver(o);
        p.getSolution().setLabel(fileName);
        p.getSolution().setAssembler(a);
        
        o.setModel(p.getModel());
        o.setSolution(p.getSolution());
        
        //a.init();
        /*
        FrameElement elm0 = (FrameElement) p.getModel().getElementsList().get(0);
        //FrameProblemDriver pd1 = (FrameProblemDriver)elm1.getMyProblemDriver();
        //System.out.println("Elm1ProblemDriver = " + elm1.getMyProblemDriver().getClass());
        System.out.println("K1 = ");
        IMatrix k = elm0.getC();
        System.out.println(k);
        */
        //System.out.println(a.getCuu());
        
        p.getSolution().execute();
        //writeResults(p);
        
	}
    
    public static void writeResults(PersistenceAsXML_XSD p){
    	//Creating formator
		DecimalFormat fmt = new DecimalFormat();
		DecimalFormatSymbols fsym = new DecimalFormatSymbols();
		fsym.setDecimalSeparator('.');
		fmt.setDecimalFormatSymbols(fsym);
		
		//Nodal displacements
        System.out.println("Displacements");
        fmt.applyPattern(".0000E00");
        System.out.print("Node \t");
        Node node1 = (Node)p.getModel().getNodesList().get(0);
        for (int i = 0; i < node1.getNumberOfDegreesOfFreedom(); i++) {
        	System.out.print(node1.getNodeValues(node1.DOF_LABELS).getStringPointValue(i) + "\t\t");
    	}
        System.out.println(" ");
        ListIterator nodes = p.getModel().getNodesList().listIterator();
        while (nodes.hasNext()) {
        	Node node = (Node)nodes.next();
        	System.out.print(node.getLabel());
        	for (int i = 0; i < node1.getNumberOfDegreesOfFreedom(); i++) {
        		System.out.print("\t" + fmt.format(node.getNodeValues(Node.STATE_VARIABLE).getDoublePointValue(i)));
        	}
        	System.out.println(" ");
        }
        System.out.println(" ");
        
        //Nodal Reaction
        System.out.println("Reactions");
        fmt.applyPattern("##0.000");
        System.out.print("Node \t");
        Element elm1 = (Element)p.getModel().getElementsList().get(0);
        for (int i = 0; i < elm1.getAnalysisModel().getNumberOfDualInternalVariables(); i++) {
        	System.out.print(elm1.getAnalysisModel().getDualInternalVariablesLabels()[i] + "\t\t");
    	}
        System.out.println(" ");
        nodes = p.getModel().getNodesList().listIterator();
        while (nodes.hasNext()) {
        	Node node = (Node)nodes.next();
        	System.out.print(node.getLabel() + "\t");
        	for (int i = 0; i < node1.getNumberOfDegreesOfFreedom(); i++) {
        		System.out.print(fmt.format(node.getNodeValues(Node.UNKNOWN_DUAL_VARIABLE).getDoublePointValue(i))+ "\t\t");
        	}
        	System.out.println(" ");
        }
    }
    
}